package b2.widget
{
	import b2.net.*;
	import b2.util.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	
	public class MessageBox extends Sprite
	{
		private var onOKCallFunction:Function;
		private var onOKParams:Array;

		public function MessageBox(message:String, hasCacelButton:Boolean = false, alpha:Number = 1)
		{
			super();
			//透明背景
			var bgr:Sprite = new Sprite();
			var gfx:Graphics = bgr.graphics;
			gfx.beginFill(0x000000, 0.01);
			gfx.drawRect(0, 0, BFont.stageWidth, BFont.stageHeight);
			gfx.endFill();
			addChild(bgr);
			//消息盒子背景
			var MessageBoxGgr:Class = ResourceHelper.getInstance().getClassByName("MessageBox0");
			var mainSprite:Sprite = new MessageBoxGgr();
			mainSprite.width = 300;
			mainSprite.scaleY = mainSprite.scaleX;
			mainSprite.x = (BFont.stageWidth - mainSprite.width) / 2;
			mainSprite.y = (BFont.stageHeight - mainSprite.height) / 2;
			mainSprite.alpha = alpha;
			addChild(mainSprite);

			//信息文字
			var messageText:TextField = new TextField();
			messageText.selectable = false;
        	messageText.autoSize = TextFieldAutoSize.CENTER;
			messageText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);;
			messageText.htmlText = message;
			messageText.x = (BFont.stageWidth - messageText.width) / 2;
			messageText.y = 265;
			addChild(messageText);
			
			//确定按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			var buttonOK:BasicButton = new BasicButton(ButtonBgr, "确定");
			if (!hasCacelButton)
			{
				buttonOK.x = (BFont.stageWidth  - buttonOK.width) / 2;
			}
			else
			{
				buttonOK.x = BFont.stageWidth / 2 - buttonOK.width - 10;
			}
			buttonOK.y = 350;
			buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKClick);
			addChild(buttonOK);
			if (hasCacelButton)
			{
				var buttonCacel:BasicButton = new BasicButton(ButtonBgr, "取消");
				buttonCacel.x = BFont.stageWidth / 2 + 10;
				buttonCacel.y = buttonOK.y;
				addChild(buttonCacel);
				buttonCacel.addEventListener(MouseEvent.CLICK, onButtonCancelClick);
			}
		}
		
		private function onButtonCancelClick(event:MouseEvent):void
		{
			close();
		}
		
		public function onButtonOKClick(event:MouseEvent):void
		{
			close();
			if (onOKCallFunction != null)
			{
				onOKCallFunction.apply(null, onOKParams);
			}
		}
		
		public function setOKCall(onOKCallFunction:Function, onOkParams:Array = null):void
		{
			this.onOKCallFunction = onOKCallFunction;
			this.onOKParams = onOkParams;
		}
		
		public function close():void
        {
        	if (this.parent != null)
        	{
        		this.parent.removeChild(this);
        	}
        }
        
	}
}